import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;


public class Bullet {
	
	public static int DEBUG_Bullet_SPEED = 20;
	
	public static int STATUS_ALIVE 		= 10000;
	public static int STATUS_DEAD 		= 10001;
	public static int STATUS_FREEZE 	= 10002;
	
	public static int BULLET_WIDTH	= 	10;
	public static int BULLET_HEIGHT	= 	10;
	
	private DPoint position = new DPoint();
	
	private double dx, dy;
	private double speed;
	
	private int bulletStatus;
	private int bulletSpeed;
	
	public Bullet ( Point _basePoint , Point _destinationPoint)
	{
		position.x = _basePoint.x;
		position.y = _basePoint.y;
		
		double _dx = _destinationPoint.x - position.x;
		double _dy = _destinationPoint.y - position.y;
		
		bulletSpeed = DEBUG_Bullet_SPEED;
		
		dx = bulletSpeed * Math.cos(Math.atan2(_dy, _dx));
		dy = bulletSpeed * Math.sin(Math.atan2(_dy, _dx));
		
		//System.out.println("dx : " + dx  + " dy : " + dy);
		bulletStatus = STATUS_ALIVE;
	}
	
	public Bullet ( Point _basePoint , int _destX , int _destY)
	{
		position.x = _basePoint.x;
		position.y = _basePoint.y;
		
		double _dx = _destX - position.x;
		double _dy = _destY - position.y;
		
		bulletSpeed = DEBUG_Bullet_SPEED;
		
		dx = bulletSpeed * Math.cos(Math.atan2(_dy, _dx));
		dy = bulletSpeed * Math.sin(Math.atan2(_dy, _dx));
		
		//System.out.println("dx : " + dx  + " dy : " + dy);
		bulletStatus = STATUS_ALIVE;
	}
	public void findCollision(ArrayList<Enemy> enemyList)
	{
		for (int  i = 0 ; i < enemyList.size(); i++)
		{
			if ( enemyList.get(i).getEnemyRect().intersects(
					position.x, position.y, BULLET_WIDTH, BULLET_HEIGHT) )
			{
				enemyList.get(i).setEnemyStatus(Enemy.ENEMY_CRUSHED);
				bulletStatus = STATUS_DEAD;
				
			}
		}
	}
	
	public void findCollision(Player player)
	{
		if ( player.getPlayerRectangle().intersects(position.x , position.y, BULLET_WIDTH , BULLET_HEIGHT) )
		{
			player.setPlayerStatus(Player.PLAYER_STATUS_CRUSHED);
		}
	}
	
	public DPoint getMissilePosition()
	{
		return position;
	}
	
	public void update()
	{
		position.x += dx;
		position.y += dy;
		
		if ( ((int)position.x ) < 0 )
		{
			bulletStatus = STATUS_DEAD;
		}
		
		if ( ((int)position.y ) < 0 )
		{
			bulletStatus = STATUS_DEAD;
		}
		
		if ( ((int)position.x ) > MainFrame.WINDOW_SIZE_X)
		{
			bulletStatus = STATUS_DEAD;
		}
		
		if ( ((int)position.y ) > MainFrame.WINDOW_SIZE_Y)
		{
			bulletStatus = STATUS_DEAD;
		}
		
		if ( bulletStatus ==  STATUS_DEAD )
		{
			dx = 0 ; dy = 0;
		}
	}

	public int getBulletStatus() {
		// TODO Auto-generated method stub
		return bulletStatus;
	}
	
	public void setdp(double _dx , double _dy)
	{
		dx = _dx;
		dy = _dy;
	}
}
